<?php
//echo "a";
/*****************************************************
* Copyright     : www.litou.net & 喱头作品
* FileName      : appDB.class.php
* Author        : 袁龙 NickName : 无喱头
* Version       :  v1.1
* Description   :  数据库操作附加类
* CreatDate     : 2005-5-28
* HistoryEdit   : 历史修改记录
---------- 第一次修改 -------------------      
	Author  : 
	Date    : 
	Summary : 
--------------  ------------------------      
*****************************************************/

class appDB {
	
	var $db;
	var $table;
	var $pairs;

	function query ( $sql ) {
	mysql_query("Set Names '".DEFAULT_CHARSET."' ");
		if( mysql_query( $sql ) ){
			return true;
		}else{
			return false;
		}
	}

	function getInsertSQL () {
		$sql = " insert into `".$this->table."` "; 
		$sql.= " ( `" . join(array_keys($this->getDealPairs()),"`,`") . "`) "; 
		$sql.= " values "; 
		$sql.= " ('". join(array_values($this->getDealPairs()),"','")."')";
		return $sql;
	}
	



	function getUpdateSQL( $key,$val ){
		$pairs = $this->getDealPairs();
		$sql = " update `" .$this->table. "` "; 
		array_walk($pairs, create_function('&$value,$name', '$value = $name . "` = \'$value\'";'));
		$sql .= " set `" . join(array_values($pairs),",`"); 
		$sql .= " where ".$key." = '".$val."'"; 
		return $sql;
	}
	function getDeleteSQL( $key,$val,$num="1"){
		$pairs = $this->getDealPairs();
		$sql = " DELETE FROM `" .$this->table. "` "; 
		array_walk($pairs, create_function('&$value,$name', '$value = $name . "` = \'$value\'";'));
		//$sql .= "WHERE `" . join(array_values($pairs),"or`"); 
		$sql .= " where ".$key." = '".$val."'";
		$sql .= "limit $num";
		return $sql;
	}
	
	function getFieldByTable () {
		$query = mysql_query( " select * from `".$this->table."` where 1 limit 1");
		while($obj = mysql_fetch_field($query) ){
			$result[] = $obj->name;
		}
		return $result;
	}
	
	function getPrimalPairsKeys () {
		return array_keys($this->pairs);
	}

	function getNewPairsKeys () {
		foreach( $this->getPrimalPairsKeys() as $primalPairsKeys ){
			if ( in_array( $primalPairsKeys ,$this->getFieldByTable()) ) {
				$result[$primalPairsKeys] = $primalPairsKeys;
			}
		}
		return $result;
	}
	
	function getDealPairs () {
		foreach( $this->getNewPairsKeys() as $key=>$val ){
			$result[$key] = $this->pairs[$val];
		}
		return $this->daddslashes($result);
	}
	
	function daddslashes($string, $force = 0) {
		if(!$GLOBALS['magic_quotes_gpc'] || $force) {
			if(is_array($string)) {
				foreach($string as $key => $val) {
					$string[$key] = $this->daddslashes($val, $force);
				}
			} else {
				$string = addslashes($string);
			}
		}
		return $string;
	}

}
?>